

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>用户列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

  <div class="layui-fluid">   
    <div class="layui-card">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">

          <div class="layui-inline">
              <input type="text" name="id" placeholder="ID" autocomplete="off" class="layui-input">
          </div>

          <div class="layui-inline">
            <input type="text" name="account" placeholder="账号" autocomplete="off" class="layui-input">
          </div>

          <div class="layui-inline">
            <select name="role_id">
              <option value="">选择角色</option>
              {foreach $roles as $k => $vo}
              <option value="{$vo.id}">{$vo.title}</option>
              {/foreach}
            </select>
          </div>

          <div class="layui-inline">
            <select name="quick_id" lay-search>
              <option value="">选择App</option>
              {foreach $qas as $k => $vo}
              <option value="{$vo.id}">{$vo.name}</option>
              {/foreach}
            </select>
          </div>

          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
          </div>

          <div class="layui-inline" style="float: right;">
            <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
          </div>
        </div>
      </div>

      <div class="layui-card-body">
        <table id="LAY-user-back-role" lay-filter="LAY-user-back-role"></table>


        <script type="text/html" id="roleTpl">
          {{d.role.title}}
        </script>

        <script type="text/html" id="statusTpl">
          {{#  if(d.status == 1){ }}
          <span style="color: #5FB878;">正常</span>
          {{#  } else if(d.status == 0) { }}
          <span>禁用</span>
          {{#  } }}
        </script>

        <script type="text/html" id="parentTpl">
          {{#  if(d.parent){ }}
          <span style="">{{d.parent.account}}</span>
          {{#  } }}
        </script>

        <script type="text/html" id="appTpl">
          {{#  if(d.role.alias == 'agent' || d.role.alias == 'channel' || d.role.alias == 'eb'){ }}
          <span style="color: #5FB878;">{{d.app_count}}</span>
          {{#  } else { }}
          {{#  } }}
        </script>

        <script type="text/html" id="appDisableTpl">
          {{#  if(d.role.alias == 'agent' || d.role.alias == 'channel' || d.role.alias == 'eb'){ }}
          <span style="color: #5FB878;">{{d.app_count_disable}}</span>
          {{#  } else { }}
          {{#  } }}
        </script>

        <script type="text/html" id="table-useradmin-admin">

          <a class="layui-btn layui-btn-primary layui-border-orange layui-btn-xs" lay-event="switchAccount">切换账号</a>
          <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="resetPwd">重置密码</a>
        </script>
      </div>
    </div>
  </div>

 <script src="/static/layuiadmin/layui/layui.js"></script>
  <script>
  layui.config({
    base: '/static/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index','table','admin'], function(){
    var $ = layui.$
    ,form = layui.form
    ,admin = layui.admin
    ,table = layui.table;

      //监听搜索
      form.on('submit(LAY-user-back-search)', function(data){
          var field = data.field;

          //执行重载
          table.reload('LAY-user-back-role', {
              where: field
              ,page: {
                curr: 1  // 将页码重置为第一页
              }
          });
      });



    table.render({
      elem: '#LAY-user-back-role'
      , url: '/manager_user/userList' //模拟接口
      , response: {
        statusCode: 1 //规定成功的状态码，本来是：0，现在统一改为1
      }
      , page: true //开启分页
      , limit: 20
      , cols: [[
        {field: 'id', width: 80, title: 'ID'}
        , {field: 'account', title: '账号'}
        , {field: 'role', title: '角色', align: 'center', templet: '#roleTpl'}
        , {field: 'last_login_time', title: '最近登录时间'}
        , {field: 'last_login_ip', title: '最近登录IP'}
        , {field: 'status', title: '状态', templet: '#statusTpl',align: 'center',width: 100}
        , {field: 'app_count', title: '已授权App',templet: '#appTpl', align: 'center', minWidth: 120,event: 'setApp', style:'cursor: pointer;'}
        , {field: 'app_count_disable', title: '禁用App',templet: '#appDisableTpl', align: 'center', minWidth: 120,event: 'setAppDisable', style:'cursor: pointer;'}
        , {field: 'parent', title: '上级用户', templet: '#parentTpl'}
        , {field: 'channel_code', title: '渠道标识'}
        , {field: 'scale', title: '内容等级'}
        , {field: 'sharing_ratio', title: '分成比例', edit: 'text'}
        , {field: 'remarks', title: '具体描述', edit: 'text'}
        , {field: 'create_time', title: '创建时间'}
        , {title: '操作', width: 240, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
      ]]
    });

    //监听单元格编辑
    table.on('edit(LAY-user-back-role)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
      console.log(obj.value); //得到修改后的值
      console.log(obj.field); //当前编辑的字段名

      admin.req({
        url: '/manager_user/edit' + obj.field
        , type: "POST"
        , data: {'id': obj.data.id, 'remarks': obj.value,'sharing_ratio': obj.value}
        , done: function (res) {
          layer.msg('修改成功', {icon: 1, time: 1000}, function () {
          });
        }, not_finish: function (res) {
          layer.msg(res.msg);
        }
      });
    });

    //监听工具条
    table.on('tool(LAY-user-back-role)', function(obj){
      var data = obj.data;
      if(obj.event === 'switchAccount'){

        $.ajax({
          url: "{:url('ManagerUser/switch_account')}",
          type: "post",
          dataType: "json",
          data: {id:obj.data.id},
          async: true,
          success: function (rs) {
            if (rs['code'] == 100) {
              parent.location.href = '/';
            } else {
              layer.msg(rs['msg']);
            }
            return false;
          }
        });

      }
      else if(obj.event === 'resetPwd'){
        layer.open({
          type: 2
          ,title: '重置密码'
          ,content: "{:url('ManagerUser/reset_pwd')}?id="+obj.data.id
          ,maxmin: true
          ,area: ['390px', '300px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submitID = 'LAY-reset-pwd-submit'
                    ,submit = layero.find('iframe').contents().find('#'+ submitID);

            //监听提交
            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){

              $.ajax({
                url:"{:url('ManagerUser/reset_pwd')}",
                type:"post",
                dataType: "json",
                data:data.field,
                async:true,
                success: function(rs){
                  if(rs['code'] == 100){

                    layer.msg('密码重置成功',function () {
                      layer.close(index); //关闭弹层
                    });

                  }else{
                    layer.msg(rs['msg']);
                  }
                  return false;
                }
              });
            });

            submit.trigger('click');
          }
        });
      }
      else if(obj.event === 'edit'){
        var tr = $(obj.tr);
        layer.open({
          type: 2
          ,title: '修改用户'
          ,offset: '15px'
          ,content: '/manager_user/managerForm.html?id=' + obj.data.id
          ,area: ['550px', '550px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

            //监听提交
            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
              var field = data.field; //获取提交的字段

              //提交 Ajax 成功后，静态更新表格中的数据
              //$.ajax({});
              admin.req({
                url: '/manager_user/editUser'
                , type: "POST"
                , data: field
                , done: function (res) {
                  layer.msg('修改成功', {icon: 1, time: 1000}, function () {
                    table.reload('LAY-user-back-role');
                    layer.close(index); //关闭弹层
                  });
                },not_finish:function (res) {
                  layer.msg(res.msg);
                }
              });
            });

            submit.trigger('click');
          }
          ,success: function(layero, index){

          }
        })
      }else if (obj.event === 'setApp'){
        layer.open({
          type: 2
          ,title: obj.data.account + '-授权App'
          ,offset: '15px'
          ,content: '/manager_user/empowerForm.html?id=' + obj.data.id
          ,area: ['615px', '500px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            layer.confirm('确认修改吗？授权后App将不能取消授权！', {
              btn: ['确定', '取消'] //按钮
            }, function () {

              var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");
              //监听提交
              iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function (data) {
                var field = data.field; //获取提交的字段
                admin.req({
                  url: '/manager_user/managerEmpowerApp'
                  , type: "POST"
                  , data: field
                  , done: function (res) {
                    layer.msg('设置成功', {icon: 1, time: 1000}, function () {
                      table.reload('LAY-user-back-role');
                      layer.close(index); //关闭弹层
                    });
                  }, not_finish: function (res) {
                    layer.msg(res.msg);
                  }
                });
              });
              submit.trigger('click');

            }, function () {

            });
          }
          ,success: function(layero, index){

          }
        })

      }else if(obj.event === 'setAppDisable')
      {
        layer.open({
          type: 2
          ,title: obj.data.account + '-禁用App授权'
          ,offset: '15px'
          ,content: '/manager_user/empowerFormDisable.html?id=' + obj.data.id
          ,area: ['615px', '500px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            layer.confirm('确认修改吗？', {
              btn: ['确定', '取消'] //按钮
            }, function () {

              var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");
              //监听提交
              iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function (data) {
                var field = data.field; //获取提交的字段
                admin.req({
                  url: '/manager_user/managerEmpowerAppDisable'
                  , type: "POST"
                  , data: field
                  , done: function (res) {
                    layer.msg('设置成功', {icon: 1, time: 1000}, function () {
                      table.reload('LAY-user-back-role');
                      layer.close(index); //关闭弹层
                    });
                  }, not_finish: function (res) {
                    layer.msg(res.msg);
                  }
                });
              });
              submit.trigger('click');

            }, function () {

            });
          }
          ,success: function(layero, index){

          }
        })
      }
    });


    //事件
    var active = {
      add: function(){
        layer.open({
          type: 2
          ,title: '添加用户'
          ,offset: '15px'
          ,content: '/manager_user/managerForm.html'
          ,area: ['550px', '550px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
            ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

            //监听提交
            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
              var field = data.field; //获取提交的字段

              //提交 Ajax 成功后，静态更新表格中的数据
              //$.ajax({});
              admin.req({
                url: '/manager_user/addUser'
                , type: "POST"
                , data: field
                , done: function (res) {
                  layer.msg('添加成功', {icon: 1, time: 1000}, function () {
                    table.reload('LAY-user-back-role');
                    layer.close(index); //关闭弹层
                  });
                },not_finish:function (res) {
                  layer.msg(res.msg);
                }
              });
            });

            submit.trigger('click');
          }
        });
      }
    }
    $('.layui-btn.layuiadmin-btn-role').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
  </script>
</body>
</html>

